package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.ValueAddedOrder;
import com.ruoyi.yipaiju.domain.ValueAddedOrderFollower;
import com.ruoyi.yipaiju.service.IValueAddedOrderFollowerService;
import com.ruoyi.yipaiju.service.IValueAddedOrderService;
import com.ruoyi.yipaiju.service.IXcxUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 增值
 */
@RestController
@RequestMapping("/valueadded")
public class ValueAddedController extends BaseController {


    @Autowired
    private IValueAddedOrderService valueAddedOrderService;
    @Autowired
    private IValueAddedOrderFollowerService valueAddedOrderFollowerService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 生成增值订单
     * @param valueAddedOrder
     * @return
     */
    @PostMapping("/orders")
    public ApiResult orders(@RequestBody ValueAddedOrder valueAddedOrder) {
        valueAddedOrder.setNo(CurrencyUtil.create(10));
        valueAddedOrder.setClient(SecurityUtils.getUserId());
        valueAddedOrderService.insertValueAddedOrder(valueAddedOrder);
        return ApiResult.success();
    }

    /**
     * 增值预约详情
     * @param id
     * @return
     */
    @GetMapping("/orders/{id}")
    public ApiResult ordersInfo(@PathVariable("id") Long id){
        ValueAddedOrder valueAddedOrder = valueAddedOrderService.selectValueAddedOrderById(id);

        List<ValueAddedOrderFollower> valueAddedOrderFollowers = valueAddedOrderFollowerService.selectValueAddedOrderFollowerList(Builder.of(ValueAddedOrderFollower::new).with(ValueAddedOrderFollower::setValueAddedOrderId, valueAddedOrder.getId()).build());
        valueAddedOrder.setValueAddedOrderFollowers(valueAddedOrderFollowers);

        List<Long> ids = valueAddedOrderFollowers.stream().map(item -> Long.valueOf(item.getMarketer())).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        valueAddedOrderFollowers.stream().forEach(item -> {
            XcxUser xcxUser = xcxUserMap.get(Long.valueOf(item.getMarketer()));
            if (xcxUser != null){
                item.setImg(xcxUser.getAvatarUrl());
                item.setMarkName(xcxUser.getNickname());
                item.setTelphone(xcxUser.getPhone());
            }
        });
        return ApiResult.success(valueAddedOrder);
    }

    /**
     * 增值跟单
     * @return
     */
    @PostMapping("/orders/followers")
    public ApiResult ordersFollowers(@RequestBody ValueAddedOrderFollower valueAddedOrderFollower){
        if (StringUtils.isEmpty(valueAddedOrderFollower.getMarketer())){
            valueAddedOrderFollower.setMarketer(SecurityUtils.getUserId().toString());
        }
        ValueAddedOrderFollower valueAddedOrderFollower1 = valueAddedOrderFollowerService.selectOneValueAddedOrderFollower(valueAddedOrderFollower);
        //  TODO 只能一个人接单  这个判断有问题  再说吧

        if (valueAddedOrderFollower1 == null){
            valueAddedOrderFollower1 = Builder.of(ValueAddedOrderFollower::new)
                    .with(ValueAddedOrderFollower::setValueAddedOrderId, valueAddedOrderFollower.getValueAddedOrderId())
//                    .with(ForeclosedHouseBookingorderFollower::setMarketer, SecurityUtils.getUserId().toString())
                    .with(ValueAddedOrderFollower::setMarketer, valueAddedOrderFollower.getMarketer())
                    .with(ValueAddedOrderFollower::setStage, 1)
                    .build();
            valueAddedOrderFollowerService.insertValueAddedOrderFollower(valueAddedOrderFollower1);
            ValueAddedOrder valueAddedOrder = valueAddedOrderService.selectValueAddedOrderById(valueAddedOrderFollower.getValueAddedOrderId());
            valueAddedOrder.setStage(2);
            valueAddedOrder.setServingTime(new Date());
            valueAddedOrderService.updateValueAddedOrder(valueAddedOrder);
        }
        ValueAddedOrder valueAddedOrder = valueAddedOrderService.selectValueAddedOrderById(valueAddedOrderFollower1.getValueAddedOrderId());
        if (valueAddedOrder != null) {
            valueAddedOrderFollower1.setValueAddedOrder(valueAddedOrder);
            return ApiResult.success(valueAddedOrderFollower1);
        }
        return ApiResult.error();
    }

    /**
     * 设置备注和下一步状态更新
     * @param id
     * @param valueAddedOrderFollower
     * @return
     */
    @PostMapping("/orders/followers/{id}")
    public ApiResult ordersFollowersInfo(@PathVariable("id") Long id, @RequestBody ValueAddedOrderFollower valueAddedOrderFollower){
        ValueAddedOrderFollower valueAddedOrderFollower1 = valueAddedOrderFollowerService.selectValueAddedOrderFollowerById(id);
        if (valueAddedOrderFollower1 != null){
            if (valueAddedOrderFollower.getRemark() != null){
                valueAddedOrderFollower1.setRemark(valueAddedOrderFollower.getRemark());
            }
            if (valueAddedOrderFollower.getStage() != null){
                valueAddedOrderFollower1.setStage(valueAddedOrderFollower.getStage());
                if (valueAddedOrderFollower.getStage().intValue() == 2){
                    valueAddedOrderFollower1.setGetDemandTime(new Date());
                }
                if (valueAddedOrderFollower.getStage().intValue() == 3){
                    valueAddedOrderFollower1.setOlInviteTime(new Date());
                }

                if (valueAddedOrderFollower.getStage().intValue() == 4){
                    valueAddedOrderFollower1.setInterviewTime(new Date());
                }
                if (valueAddedOrderFollower.getStage().intValue() == 5){
                    valueAddedOrderFollower1.setFinishTime(new Date());
                    ValueAddedOrder valueAddedOrder = valueAddedOrderService.selectValueAddedOrderById(valueAddedOrderFollower1.getValueAddedOrderId());
                    valueAddedOrder.setStage(3);
                    valueAddedOrder.setFinishTime(new Date());
                    valueAddedOrder.setFinished(1);
                    valueAddedOrderService.updateValueAddedOrder(valueAddedOrder);
                }
            }

            ValueAddedOrder valueAddedOrder = valueAddedOrderService.selectValueAddedOrderById(valueAddedOrderFollower1.getValueAddedOrderId());
            valueAddedOrderFollower1.setValueAddedOrder(valueAddedOrder);
            valueAddedOrderFollowerService.updateValueAddedOrderFollower(valueAddedOrderFollower1);
            return ApiResult.success(valueAddedOrderFollower1);
        }
        return ApiResult.error();
    }
}
